﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace myNico
{
   public class tool
    {
       public static string speedConvert(long value)
       {
           double speed;
           if (value == 0) speed = 0;
           else
           {
               if (!double.TryParse(value.ToString(), out speed))
               {
                   speed = 0;
               }
           }
           if (speed > 1024 * 1024)
           {
               speed = speed / (1024 * 1024);
               return string.Format("{0} MB", speed.ToString("0.00"));
           }
           else if (speed > 1024)
           {
               speed = speed / (1024);
               return string.Format("{0} KB", speed.ToString("0.00"));
           }
           else
           {
               return string.Format("{0} B", speed.ToString("0.00"));
           }
       }

       /// <summary>
       /// 日志
       /// </summary>
       /// <param name="str"></param>
       public static void wf_log(string str)
       {
           
           FileStream fs = new FileStream("Log.txt", FileMode.Append,FileAccess.Write,FileShare.Write);
          StreamWriter swt = new StreamWriter(fs);
          swt.Write(DateTime.Now.ToString() + swt.NewLine);
          
          swt.WriteLine(str);
          swt.Close();
          fs.Close();
       }

       
           /// <summary>
           /// Sets the cert policy.
           /// </summary>
           public static void SetCertificatePolicy()
           {
               ServicePointManager.ServerCertificateValidationCallback
                          += RemoteCertificateValidate;
           }

           /// <summary>
           /// Remotes the certificate validate.
           /// </summary>
           private static bool RemoteCertificateValidate(
              object sender, X509Certificate cert,
               X509Chain chain, SslPolicyErrors error)
           {
               // trust any certificate!!!
               System.Console.WriteLine("Warning, trust any certificate");
               return true;
           }
       
    }
}
